package com.texas_android.app.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.texas_android.app.Appconfig.AppContext;
import com.texas_android.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by solin on 2018/3/19.
 */

public class DialogHelper {
    /**
     * @param msg
     * @param context
     * @return
     */
    public static Dialog loadingDialog(String msg, Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.item_dialog_loadding, null);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        if (!TextUtils.isEmpty(msg))
            tv_content.setText(msg);
        Dialog loadingDialog = new Dialog(context, R.style.loadingDialog);
        loadingDialog.setCancelable(false);
        loadingDialog.setContentView(view);
        return loadingDialog;
    }


    /**
     * 列表dialog
     */
    public static class ZXQbListDialog extends Dialog {
        private TextView mTitle;
        private ListView mListView;
        private Context mContext;
        private MyArrayListAdapter mAdapter;

        private OnItemSelectListenter myItemSelectListener;

        public ZXQbListDialog(Context context, int layoutId) {
            super(context, R.style.dialog_style);
            this.mContext = context;
            View view = View.inflate(context, layoutId, null);
            mTitle = (TextView) view.findViewById(R.id.tv_itemTitle);
            mListView = (ListView) view.findViewById(R.id.listView);
            setContentView(view);
            getWindow().getAttributes().width = (int) (AppContext.getScreenWidth(context) * 0.2);

        }

        public void setmTitle(String title) {
            if (TextUtils.isEmpty(title))
                mTitle.setText("请选择");
            mTitle.setText(title);
        }

        public void setHeight(float height) {
//            if (height > 0)
//                getWindow().getAttributes().height = (int) (AppContext.getScreenWidth(mContext) * 0.2);
        }

        public void setMyItemSelectListener(OnItemSelectListenter myItemSelectListener) {
            this.myItemSelectListener = myItemSelectListener;
        }

        /**
         * 设置数据
         *
         * @param mDatas
         */
        public void setListDatas(List<String> mDatas) {
            mAdapter = new MyArrayListAdapter(mDatas, mContext);
            mListView.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        }

        /**
         * 设置点击
         *
         * @param onItemClick
         */
        public void setOnItemClick(AdapterView.OnItemClickListener onItemClick) {
            mListView.setOnItemClickListener(onItemClick);
        }

        /**
         * 能处理的返回0
         * 不能处理的返回-1
         *
         * @param searchText
         * @return
         */

        public int voiceSearchKey(String searchText) {
            Log.i("Dialog查询结果：", searchText);
            if (searchText.contains("返回桌面") || searchText.contains("返回系统")) {
                AppContext.closeActivityAll();
                return 0;
            }
            if (searchText.contains("选择第一条")) {
                myItemSelectListener.mySelect(0);
            } else if (searchText.contains("选择第二条")) {
                myItemSelectListener.mySelect(1);
            } else if (searchText.contains("选择第三条")) {
                myItemSelectListener.mySelect(2);
            } else if (searchText.contains("选择第四条")) {
                myItemSelectListener.mySelect(3);
            } else if (searchText.contains("选择第五条")) {
                myItemSelectListener.mySelect(4);
            } else if (searchText.contains("选择第六条")) {
                myItemSelectListener.mySelect(5);
            } else if (searchText.contains("选择第七条")) {
                myItemSelectListener.mySelect(6);
            } else if (searchText.contains("选择第条")) {
                myItemSelectListener.mySelect(-1);
            }
            if (!TextUtils.isEmpty(searchText) && searchText.length() >= 2) {

            }
            return 0;
        }
    }

    private static class MyArrayListAdapter extends BaseAdapter {
        private List<String> mDatas = new ArrayList<>();
        private Context mContext;


        public MyArrayListAdapter(List<String> mDatas, Context mContext) {
            this.mDatas = mDatas;
            this.mContext = mContext;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyView myView = null;
            if (myView == null) {
                myView = new MyView();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_spinner, null);
                myView.textView = (TextView) convertView;
                convertView.setTag(myView);
            } else {
                myView = (MyView) convertView.getTag();
            }
            myView.textView.setText(Html.fromHtml(mDatas.get(position)));
            return convertView;
        }

        private class MyView {
            TextView textView;
        }
    }

    /**
     * 显示列表选择
     *
     * @param mContext
     * @return
     */
    public static ZXQbListDialog openZxListDialog(
            Context mContext,
            List<String> mItems,
            String mTitle,
            float height,
            AdapterView.OnItemClickListener onItemClick, OnItemSelectListenter myItemSelect) {
        ZXQbListDialog dialog = new ZXQbListDialog(mContext, R.layout.item_view_zxlist);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setmTitle(mTitle);
        dialog.setListDatas(mItems);
        dialog.setHeight(height);
        dialog.setOnItemClick(onItemClick);
        dialog.show();
        dialog.setMyItemSelectListener(myItemSelect);
        return dialog;
    }

    public interface OnItemSelectListenter {
        void mySelect(int dex);
    }

    /**
     * =============================================================================================================
     */
    /**
     * ========================================版本升级 Dialog======================================================
     */
    public static class UpdateDialog extends Dialog {

        private TextView tv_update_version_message;
        private ImageView iv_update_cancel;
        private TextView tv_update_button;

        public UpdateDialog(Context context, int layout) {
            super(context, R.style.dialog_style);
            View content = View.inflate(context, layout, null);
            iv_update_cancel = (ImageView) content.findViewById(R.id.iv_dialog_icon);
            tv_update_version_message = (TextView) content.findViewById(R.id.tv_update_version_message);
            tv_update_button = (TextView) content.findViewById(R.id.tv_update_button);
            setContentView(content);
            setCancelable(true);
            setCanceledOnTouchOutside(false);
            getWindow().getAttributes().width = (int) (AppContext.getScreenWidth(context) * 0.3);
        }

        public void setUpdateListener(View.OnClickListener listener) {
            this.tv_update_button.setOnClickListener(listener);
        }

        public void setCancelListener(View.OnClickListener listener) {
            if (listener == null)
                iv_update_cancel.setVisibility(View.GONE);
            else
                iv_update_cancel.setOnClickListener(listener);
        }
    }

    public static UpdateDialog openUpdateDialogWithCancel(
            Context mContext, String version_message) {
        UpdateDialog dialog = new UpdateDialog(mContext, R.layout.view_update_dialog);
        dialog.tv_update_version_message.setText(version_message);
        dialog.show();
        return dialog;
    }
    /**
     * =============================================================================================================
     */
    /**
     * ========================================版本升级 ProgressDialog======================================================
     */
    public static class UpdateProgressDialog extends Dialog {

        private TextView tv_progress_value;
        public ProgressBar progressBar;
        private TextView tv_version_message;

        public UpdateProgressDialog(Context context, int layout) {
            super(context, R.style.dialog_style);
            View content = View.inflate(context, layout, null);
            tv_progress_value = (TextView) content.findViewById(R.id.tv_progress_value);
            progressBar = (ProgressBar) content.findViewById(R.id.progressBar);
            tv_version_message = (TextView) content.findViewById(R.id.progress_message);
            setContentView(content);
            setCancelable(true);
            setCanceledOnTouchOutside(false);
            getWindow().getAttributes().width = (int) (AppContext.getScreenWidth(context) * 0.3);
        }

        public void setProgressBar(int progress) {
            progressBar.setProgress(progress);
            tv_progress_value.setText(progress + "%");
        }
    }

    public static UpdateProgressDialog openUpdateProgressDialog(
            Context mContext, String version_message) {
        UpdateProgressDialog dialog = new UpdateProgressDialog(mContext, R.layout.layout_update_progress);
        dialog.tv_version_message.setText(version_message);
        dialog.show();
        return dialog;
    }
}
